package com.krain.ddbb.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.krain.corelibrary.widget.recyleview.RefreshRecyclerView;
import com.krain.corelibrary.widget.recyleview.implement.OnItemClickLisener;
import com.krain.corelibrary.widget.recyleview.implement.OnRecyclerItemListener;
import com.krain.ddbb.R;
import com.krain.ddbb.activity.OrderDetailActivity_;
import com.krain.ddbb.adapter.UserIssueAdapter;
import com.krain.ddbb.entity.AppApi;
import com.krain.ddbb.entity.BaseListEntity;
import com.krain.ddbb.entity.UserIssueBean;
import com.orhanobut.logger.Logger;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * Class description
 * 订单列表的fragment
 *
 * @author yamin
 * @date 2016/1/29 下午 12:55
 */
@EFragment(R.layout.fragment_orderlist)
public class OrderListFragment extends BaseFragment implements RefreshRecyclerView.SwipeRefreshListener {

    @ViewById(R.id.act_userorder_recyleview)
    RefreshRecyclerView mRefreshRecyclerView;
    private List<UserIssueBean> mEntityList;
    int pageIndex = 1;
    int maxPage;
    @FragmentArg("type")
    int type;
    public static final int TYPE_PUBLISH = 0;//发布的订单
    public static final int TYPE_ROBED = 1;//抢到的订单


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @AfterViews
    void afterView() {
        mRefreshRecyclerView.getRecyclerView().setHasFixedSize(true);
        mRefreshRecyclerView.showRefreshing(true);
        mRefreshRecyclerView.setSwipeRefreshListener(this);

    }


    /**
     * 请求发布订单列表
     */
    @Background
    protected void getPublishOrderList() {
        BaseListEntity<UserIssueBean> listEntity = AppApi.getsInstance(mBaseActivity).getIssueList(app.getmUserinfo().getAccess_token(), pageIndex);
        mEntityList = listEntity.getList();
        maxPage =  listEntity.getPage();

        if (mEntityList != null) {
            if (mEntityList.size() == 0) {
                baseUtil.showSnackBar(mBaseActivity, "暂时没有新的数据");
            }
            setAdapter(mEntityList, mRefreshRecyclerView);
        }

    }


    /**
     * 请求服务订单列表
     */
    @Background
    protected void getServerOrder() {
        mEntityList = AppApi.getsInstance(mBaseActivity).getServerList(app.getmUserinfo().getAccess_token(), pageIndex).getList();
        maxPage =  AppApi.getsInstance(mBaseActivity).getIssueList(app.getmUserinfo().getAccess_token(), pageIndex).getPage();
        if (mEntityList != null) {
            if (mEntityList.size() == 0) {
                baseUtil.showSnackBar(mBaseActivity, "暂无数据");
            }
            setAdapter(mEntityList, mRefreshRecyclerView);
        }

    }

    UserIssueAdapter adapter;

    @UiThread
    protected void setAdapter(List<UserIssueBean> mEntityList, RefreshRecyclerView mRefreshRecyclerView) {
        if (adapter == null) {
            if (mEntityList.size()>10)   mRefreshRecyclerView.setIsLoadMore(true);
            adapter = new UserIssueAdapter(mEntityList, mBaseActivity);
            mRefreshRecyclerView.setAdapter(adapter);
            adapter.setOnItemListener(new MyRecyclerItemListener(mEntityList));
            mRefreshRecyclerView.setRefreshing(false);
        } else {
            if (maxPage == pageIndex) mRefreshRecyclerView.setIsLoadMore(false);
            List<UserIssueBean> list = adapter.getList();
            list.addAll(mEntityList);
            adapter.refreshList(list);
            mRefreshRecyclerView.complete();
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        adapter = null;
        getOrderList();
    }

    @Override
    public void onRefresh() {
        pageIndex = 0;
        adapter = null;
        getOrderList();
    }

    void getOrderList() {
        if (type == TYPE_PUBLISH) {
            getPublishOrderList();
        } else {
            getServerOrder();
        }
    }

    @Override
    public void onLoadMore() {
        if (!mRefreshRecyclerView.getCanLoadMore()) return;
        pageIndex++;
        getOrderList();
    }


    /**
     * 列表的点击事件
     */
    class MyRecyclerItemListener extends OnRecyclerItemListener {
        List<UserIssueBean> mEntityList;

        public MyRecyclerItemListener(List<UserIssueBean> mEntityList) {
            this.mEntityList = mEntityList;
        }

        @Override
        public void onItemClick(int position, int viewType, RecyclerView.ViewHolder holder, View v) {
            Intent intent = new Intent(getActivity(), OrderDetailActivity_.class);
            UserIssueBean bean = mEntityList.get(position);
            bean.setCurrentIdStatus(UserIssueBean.CURRENTSTATUS_CLIENT);
            intent.putExtra("orderBean", bean);
            intent.putExtra("id", String.valueOf(bean.getId()));
            intent.putExtra("type", type);
            getActivity().startActivity(intent);
        }
    }
}
